package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.Question;
import com.hejia.alauda.service.QuestionService;
import com.hejia.alauda.utils.HttpResponse;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;

@Controller
@RequestMapping("question")
public class QuestionController extends BaseController {

	private static final Log logger = LogFactory
			.getLog(QuestionController.class);

	@Resource
	private QuestionService questionService;

	@RequestMapping
	public String list(Model model, Pager<Question> pager, String ask) {

		pager = this.questionService.findQuestions(pager, ask);
		model.addAttribute("ask", ask);
		model.addAttribute("questions", pager);

		// 分页
		PagerHelper<Question> helper = new PagerHelper<>();
		helper.setPager(pager);
		Map<String, String> map = new HashMap<>();
		if (ask != null) {
			map.put("ask", ask);
		}
		helper.setParams(map);
		helper.setBaseUrl("/question");
		model.addAttribute("pagerHelper", helper);

		return "question/list";
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse add(Question question) {

		HttpResponse response = new HttpResponse();

		try {
			question = questionService.addQuestion(question);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("添加问题成功");
			response.setData(question);

		} catch (Exception e) {
			logger.error("添加问题失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("添加问题失败 " + e.getMessage());
			response.setData(question);
		}

		return response;
	}

	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public HttpResponse delete(@PathVariable Long id) {

		HttpResponse response = new HttpResponse();

		try {
			questionService.deleteQuestion(id);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("删除问题成功");

		} catch (Exception e) {
			logger.error("删除问题失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("删除问题失败 " + e.getMessage());
		}

		return response;
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Long id, ModelMap model) {
		Question question = questionService.findQuestionById(id);
		model.put("question", question);
		return "/question/update";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "edit/{id}", method = RequestMethod.POST)
	public String edit(@PathVariable Long id, Question question,
			RedirectAttributes redirectAttributes) {
		Question question2 = questionService.findQuestionById(id);
		question2.setAsk(question.getAsk());
		question2.setAnswer(question.getAnswer());
		question2.setModifyDate(new Date());

		if (questionService.editQuestion(question2)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "问题修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "问题修改失败");
		}

		return "redirect:/question";
	}

}
